package br.com.sadim.web;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.faces.FacesException;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.imageio.stream.FileImageOutputStream;
import javax.servlet.ServletContext;

import org.primefaces.event.CaptureEvent;

import br.com.sadim.hospital.StatusConsulta;
import br.com.sadim.hospital.TipoUsuario;
import br.com.sadim.hospital.Usuario;
import br.com.sadim.paciente.Paciente;
import br.com.sadim.paciente.PacienteRN;
import br.com.sadim.paciente.SexoPaciente;

public class PacienteBean {
	private Paciente paciente = new Paciente();
	private List<Paciente> lista;
	private PacienteRN pacienteRN = new PacienteRN();
	

	
	
	private List<String> photos = new ArrayList<String>();  
	
	
	
    
    private String getRandomImageName() {  
        int i = (int) (Math.random() * 10000000);  
          
        return String.valueOf(i);  
    }  
  
    public List<String> getPhotos() {  
        return photos;  
    }      
      
    public void oncapture(CaptureEvent captureEvent) {  
     //   String photo = getRandomImageName();  
        this.photos.add(0,"foto");  
        byte[] data = captureEvent.getData();  
          
        ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();  
        String newFileName = servletContext.getRealPath("") + File.separator + "foto" + ".png";  
        FileImageOutputStream imageOutput;  
        try {  
            imageOutput = new FileImageOutputStream(new File(newFileName));  
            imageOutput.write(data, 0, data.length);  
            imageOutput.close();  
        }  
        catch(Exception e) {  
            throw new FacesException("Error in writing captured image.");  
        }  
    }  
	
	
	
	public String salvar() {
		ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();  
        String newFileName = servletContext.getRealPath("") + File.separator + "foto" + ".png";  
        File file = new File(newFileName);
        byte[] bFile = new byte[(int) file.length()];
 
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            fileInputStream.read(bFile);
            fileInputStream.close();
            
            File f = new File(newFileName);
            f.delete();
        } catch (Exception e) {
            e.printStackTrace();
        }
		
        paciente.setFoto(bFile);
		paciente.setStatus(true);
		
		pacienteRN.salvar(getPaciente());
		
		FacesContext context = FacesContext.getCurrentInstance();
        context.addMessage(null, new FacesMessage("Dados cadastrados com sucesso"));  


		return "listagemPaciente";
	}
	
	public String edit(){
		try{
        	ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();  
            String newFileName = servletContext.getRealPath("") + File.separator + "foto" + ".png";  
            FileOutputStream fos = new FileOutputStream(newFileName);
            fos.write(paciente.getFoto());
            fos.close();
        }catch(Exception e){
            e.printStackTrace();
        }
		return "paciente";
	}
	
	public String delete(){
		
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Paciente Excluido",  "Paciente excluido com sucesso");  
        
        FacesContext.getCurrentInstance().addMessage(null, message);
                
		paciente.setStatus(false);
		pacienteRN.salvar(getPaciente());
		//pacienteRN.delete(getPaciente());
		return "listagemPaciente";
	}


	public List<Paciente> getLista() {
			lista = pacienteRN.getList();
			if(lista==null) {
		}
		return lista;
	}

	public List<SelectItem> getListaPacientes() {
		List<SelectItem> pacientes = new ArrayList<SelectItem>();
		for(Paciente u : getLista()) {
			pacientes.add(new SelectItem(u.getIdPaciente(), u.getNome()));

		}
		return pacientes;
	}
	
	public List<SelectItem> getListSexoPaciente(){
		List<SelectItem> tipos = new ArrayList<SelectItem>();  
        
        for (SexoPaciente s : SexoPaciente.values()) {  
            tipos.add(new SelectItem(s, s.getTipo()));  
        }  
          
        return tipos;	
	}
	

	
	public String novoPaciente() {
		//Deleta o arquivo temporario de foto
		ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();  
        String newFileName = servletContext.getRealPath("") + File.separator + "foto" + ".png";  
				File f = new File(newFileName);
		        f.delete();
		setPaciente(new Paciente());
		paciente.setStatus(true);
		return "paciente";
	}

	public Paciente getPaciente() {
		return paciente;
	}

	public void setPaciente(Paciente paciente) {
		this.paciente = paciente;
	}


}
